﻿using System.Collections.Generic;
using System.Linq;
using SIG.Repository;
using SIG.Model;

using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Configs;
using SIG.Resources.Admin;

namespace SIG.Service
{
    public class ProductPhotoService: EntityService<ProductPhoto>, IProductPhotoService
    {
       
        private readonly IProductPhotoRepository _productPhotoRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public ProductPhotoService( IProductPhotoRepository productPhotoRepository, ILoggingService loggingService, 
            ICacheService cacheService)
            : base(productPhotoRepository, cacheService, loggingService)
        {
           
            _productPhotoRepository = productPhotoRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }

        
        public IList<ProductPhoto> GetPhotos(int productId)
        {        
            var key = $"{EntityNames.ProductPhoto}s_ByProduct_{productId}";

            if (!SettingsManager.Product.EnableCaching)
            {
                return _productPhotoRepository.GetFilteredElements((p => p.ProductId == productId)).ToList();
            }

            

            if (_cacheService.IsSet(key))
            {
                return (List<ProductPhoto>)_cacheService.Get(key);

            }

            List<ProductPhoto> photos = _productPhotoRepository.GetFilteredElements((p => p.ProductId == productId)).ToList();
                _cacheService.Set(key, photos, SettingsManager.Product.CacheDuration);
         

            return photos;
        }
        

       
    }
}
